package array;

/**
 * @ Author     ：侯耀祖
 * @ Description：
 */
public class generateMatrix {
    public int[][] generateMatrix(int n) {
        int[][] matrix = new int[n][n];
        int round = n/2;
        int startx = 0,starty = 0;
        int i,j;
        int len = 1;
        int count = 1;
        while (round > 0){
            i = startx;
            j = starty;
            for (; j < n-len; j++) {
                matrix[i][j] = count;
                count++;
            }
            for (; i < n-len; i++) {
                matrix[i][j] = count;
                count++;
            }
            for (; j > starty; j--){
                matrix[i][j] = count;
                count++;
            }
            for (; i > startx; i--){
                matrix[i][j] = count;
                count++;
            }
            len++;
            startx++;
            starty++;
            round--;
        }
        if (n%2 != 0){
            matrix[n/2][n/2] = count;
        }
        return matrix;
    }
}
